<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\wechat\service;

use app\wechat\common\WechatException;

final class User
{
    public function download_user_list($mp_number)
    {
        $wxobj = service('wechat/mp')->get_wx_object($mp_number);

        $next_openid = '';

        while (true) {
            $result = $wxobj->getUserList($next_openid);
            if ($result === false) {
                throw new WechatException($wxobj);
            }

            if (isset($result['data']['openid']) && $result['data']['openid']) {
                foreach ($result['data']['openid'] as $openid) {
                    $user_info = model('wechat/user')->where(array('mp_number' => $mp_number, 'openid' => $openid))->find();
                    if ($user_info === null) {
                        $user_info = model('wechat/user')->initDefault();
                        $user_info['mp_number'] = $mp_number;
                        $user_info['openid'] = $openid;
                        $user_info->save();
                        $this->update_user_info($mp_number, $openid);
                    }
                }
            }

            if (isset($result['next_openid']) && $result['next_openid']) {
                $next_openid = $result['next_openid'];
            } else {
                break;
            }
        }
    }

//     public function add_openid_to_db($mp_number, $openid)
//     {
//         $user_info = model('wechat/user')->where(array('mp_number' => $mp_number, 'openid' => $openid))->find();
//         if ($user_info === null) {
//             $user_info = model('wechat/user')->initDefault();
//             $user_info['mp_number'] = $mp_number;
//             $user_info['openid'] = $openid;
//             $user_info->save();
//         }
//     }

//     public function remove_openid_from_db($mp_number, $openid)
//     {
//         $user_info = model('wechat/user')->where(array('mp_number' => $mp_number, 'openid' => $openid))->find();
//         if ($user_info !== null) {
//             $user_info->delete();
//         }
//     }

    public function update_user_info($mp_number, $openid)
    {
        $user_info = model('wechat/user')->where(array('mp_number' => $mp_number, 'openid' => $openid))->find();
        if ($user_info !== null) {
            $wxobj = service('wechat/mp')->get_wx_object($mp_number);

            $result = $wxobj->getUserInfo($openid);
            if ($result === false) {
                throw new WechatException($wxobj);
            }

            $user_info['nickname'] = isset($result['nickname']) ? $result['nickname'] : '';
            $user_info['subscribe'] = isset($result['subscribe']) ? $result['subscribe'] : '';
            $user_info['sex'] = isset($result['sex']) ? $result['sex'] : '';
            $user_info['city'] = isset($result['city']) ? $result['city'] : '';
            $user_info['country'] = isset($result['country']) ? $result['country'] : '';
            $user_info['province'] = isset($result['province']) ? $result['province'] : '';
            $user_info['language'] = isset($result['language']) ? $result['language'] : '';
            $user_info['headimgurl'] = isset($result['headimgurl']) ? $result['headimgurl'] : '';
            $user_info['subscribe_time'] = isset($result['subscribe_time']) ? $result['subscribe_time'] : '';
            $user_info['unionid'] = isset($result['unionid']) ? $result['unionid'] : '';
            $user_info['remark'] = isset($result['remark']) ? $result['remark'] : '';
            $user_info['tagid_list'] = isset($result['tagid_list']) ? $result['tagid_list'] : '';
            $user_info->save();
        }

        return $user_info;
    }

    public function subscribe_openid($mp_number, $openid)
    {
        $user_info = model('wechat/user')->where(array('mp_number' => $mp_number, 'openid' => $openid))->find();
        if ($user_info === null) {
            $user_info = model('wechat/user')->initDefault();
            $user_info['mp_number'] = $mp_number;
            $user_info['openid'] = $openid;
            $user_info->save();

            $this->update_user_info($mp_number, $openid);
        }
    }

    public function unsubscribe_openid($mp_number, $openid)
    {
        $user_info = model('wechat/user')->where(array('mp_number' => $mp_number, 'openid' => $openid))->find();
        if ($user_info !== null) {
            $user_info['subscribe'] = 0;
            $user_info['subscribe_time'] = 0;
            $user_info->save();
        }
    }

    public function get_user_by_openid($mp_number, $openid)
    {
        $user_info = model('wechat/user')->where(array('mp_number' => $mp_number, 'openid' => $openid))->find();
        if ($user_info === null) {
            $this->subscribe_openid($mp_number, $openid);
            $user_info = model('wechat/user')->where(array('mp_number' => $mp_number, 'openid' => $openid))->find();
        }
        return $user_info;
    }
}
